<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/
 * @license		GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

require_once (JPATH_COMPONENT_SITE.DS.'common_functions.php');

/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class TargetsModelTarget extends JModel
{
	/**
	 * Gets the greeting
	 * @return string The greeting to be displayed to the user
	 */
	var $_data;
	var $id = null;
	
	function buildQuery($id) {
		$query = 'SELECT id, target_name, p_id FROM #__trg_targets where id = ' . $id . ' order by p_id';
		return $query;
	}
	
	function buildQueryP($id) {
		if (!is_array($id)) {
			$query = 'SELECT id, target_name, p_id FROM #__trg_targets where p_id = ' . $id . ' order by p_id';
		}
		else {
			$query = 'SELECT id, target_name, p_id FROM #__trg_targets where p_id IN (' . implode(',', $id) . ') order by p_id';
		}
		return $query;
	}
	
	function getTargetName($id) {
		$db =& JFactory::getDBO();
		$query = $this->buildQuery($id);
		$db->setQuery( $query );
		$data = $this->_getList( $query );
		return $data[0]->target_name;
	}
	 
	function getData()
	{
		$db =& JFactory::getDBO();
		
		if ($this->id == null) {
			$this->id = JRequest::getVar('id', 0, '', 'int');
		}
		$id = $this->id;
		
		$parent_prefix = JRequest::getVar('prefix', 0, '', 'string');
		if (!preg_match('/^(\d+\.)*\d+$/', $parent_prefix)) {
			die ('Request Error');
		}
		#print $prefix;
		
		$query = $this->buildQuery($id);
		$db->setQuery( $query );
		$this->_data = $this->_getList( $query );
		
		$query = $this->buildQueryP($id);
		$db->setQuery( $query );
		$data = $this->_getList( $query );
		
		$stack = array();
		for ($i = 0, $n = count($data); $i < $n; $i++) {
			array_push($this->_data, $data[$i]);
			array_push($stack, $data[$i]->id);
		}
		
		$PORTION = 50;
		
		while (count($stack) > 0) {
			$array_id = array();
			for ($i = 0; $i < $PORTION && count($stack) > 0; $i++) {
				array_push($array_id, array_shift($stack));
			}
			
			$query = $this->buildQueryP($array_id);
			$db->setQuery( $query );
			$data = $this->_getList( $query );
			
			for ($i = 0, $n = count($data); $i < $n; $i++) {
				array_push($stack, $data[$i]->id);
				array_push($this->_data, $data[$i]);
			}
		}
		
		#print '<pre>';
		#print_r($this->_data);
		#print '</pre>';
		
		$prefix_numbers = explode('.', $parent_prefix);
		if ($this->_data[0]->p_id != NULL) {
			$pid = $this->_data[0]->p_id;
		}
		else {
			$pid = 'NULL';
		}
		
		
		$last = array($pid => array_pop($prefix_numbers));
		#print count($prefix_numbers);
		
		$prefix = array($pid => count($prefix_numbers) == 0 ? '' : implode('.', $prefix_numbers) . '.');
		#print_r($prefix);
		
		
		for ($i = 0, $n = count($this->_data); $i < $n; $i++) {
			$pid = $this->_data[$i]->p_id;
			$id  = $this->_data[$i]->id;
			
			if ($pid == NULL) {
				$pid = 'NULL';
			}
			
			#print "i = $i<br>";
			#if ($last[$pid] == NULL) {
			#	$last[$pid] = 1;
			#}
			
			
			
			$this->_data[$i]->n = $prefix[$pid] . $last[$pid];
			#$this->_data[$i]->n = $prefix[$pid] . $last[$pid];
			#$number = $prefix[$pid] . $last[$pid];
			
			$this->_data[$i]->n_array = explode('.', $this->_data[$i]->n);
			
			if ($last[$id] == NULL) {
				$last[$id] = 1;
				#$prefix[$id] = $number;
				$prefix[$id] = $this->_data[$i]->n . '.';
			}
			else {
				$last[$id]++;
			}
			
			$last[$pid]++;
			//$this->_data[$i]->prefix = $prefix[$pid];
		}
		
		sort_targets($this->_data);
		
		return $this->_data;
	}
}

